import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/a_yyt/models/medical_elder_entity.dart';
import 'package:ls_work/model/medicine_advice_model.dart';
import 'package:ls_work/net/response/medicine_advice/drug_list_data.dart';
import 'package:ls_work/ui/medicine/choose_medicine_elder_page.dart';
import 'package:ls_work/ui/medicine/choose_medicine_page.dart';
import 'package:ls_work/ui/select_old_page.dart';

import '../../../net/response/medicine_advice/medicine_elder_list_data.dart';
class AddPutMedicineViewModel extends BaseViewModel{

  AddPutMedicineViewModel(super.context);

  MedicineElderListData? elderInfo;

  ///用量
  String? useNum;

  PickerDataWithIdModel? useTime;

  String? morning;

  String? noon;

  String? night;

  String? beforeBed;

  DrugListData? drugInfo;

  var model = MedicineAdviceModel();

  Future<void> chooseElder() async {
    var old = await chooseMedicineElder(context);
    if(old!=null){
      elderInfo = old;
      notifyListeners();
    }
  }

  Future<void> chooseMedicine() async {
    var info = await Go().push(ChooseMedicinePage());
    if(info!=null){
      drugInfo = info;
      notifyListeners();
    }
  }

  void chooseUseTime(){
    var items = [
      PickerDataWithIdModel(pickId: '1',pickName: '早'),
      PickerDataWithIdModel(pickId: '2',pickName: '中'),
      PickerDataWithIdModel(pickId: '3',pickName: '晚'),
    ];
    DataPickerView(context, items,currentFirstData: useTime,onSubmitListener: (p1,p2,p3){
      useTime = items[p1];
      notifyListeners();
    }).show();
  }


  Future<void> submitPutMedicine() async {
    if(elderInfo==null){
      showToast("请选择长者");
      return;
    }
    if(drugInfo==null){
      showToast("请选择药品");
      return;
    }

    if(useNum.isNullOrEmpty()){
      showToast("请输入用量");
    }

    var jsonBean = {
      "morning":morning??"0",
      "noon":noon??"0",
      "night":night??"0",
      "beforeBed":beforeBed??"0",
    };
    showLoadingDialog(msg: '提交中...');
    try{
      await model.submitPutMedicine(stockId: elderInfo!.id,drugId: drugInfo!.id,usageQuantity: useNum,jsonBean: jsonBean);
      hideDialog();
      showToast('提交成功');
      finish(data: true);
    }catch(e){
      print(e);
      hideDialog();
      showToast(e.toNetError());
    }
  }

}